#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2018, Niklas Hauser
# Copyright (c) 2017, Fabian Greif
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

def init(module):
    module.name = ":platform:fsmc"
    module.description = "Flexible (Static) Memory Controller (FSMC/FMC)"

def prepare(module, options):
    device = options[":target"]
    if not device.has_driver("fsmc:stm32*"):
        return False

    module.depends(":cmsis:device", ":platform:rcc")
    return True

def build(env):
    device = env[":target"]

    properties = {}
    properties["target"] = device.identifier

    target = device.identifier
    if device.has_driver("fsmc:stm32*"):
        properties["FMC"] = "FSMC"
    else:
        properties["FMC"] = "FMC"

    if target["family"] in ["f2", "f3", "f4", "f7"]:
        properties["BCRx"] = "BCR1"
        properties["BTRx"] = "BTR1"
    else:
        properties["BCRx"] = "BCRx"
        properties["BTRx"] = "BTRx"

    env.substitutions = properties
    env.outbasepath = "modm/src/modm/platform/fsmc"

    env.template("fsmc.hpp.in")
    env.template("fsmc.cpp.in")
